<template>
  <div class="managerMana-container common-container">
    <div class="common-top">
      <span class="left">账号管理</span>

      <el-button type="primary" @click="toModify('add')">添加账号</el-button>
    </div>
    <div class="common-bot">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="name" label="名称" width="150"></el-table-column>
        <el-table-column prop="setting.freeEmployeeCount" label="免费账号员工数量"></el-table-column>
        <el-table-column prop="setting.freeStoreCount" label="免费账号门店数量"></el-table-column>
        <el-table-column prop="enable" label="是否启用">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.enable" @change="modifyEnable(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <span class="span-modify edit" @click.stop="doModify('edit',scope.row)">编辑</span>
            <span class="span-modify del" @click.stop="doModify('del',scope.row)">删除</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          v-if="showPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="nowPage"
          :page-size="limit"
          layout="total, prev, pager, next"
          :total="totalElement"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import ManaApi from "@/api/index";

export default {
  name: "ManagerMana",
  computed: {
    ...mapGetters(["name"]),
  },
  data() {
    return {
      showPage: false,
      tableData: [],
      enable: 0,
      keyword: "",
      limit: 10,
      nowPage: 1,
      roleGroupId: 0,
      gender: {
        female: "女",
        male: "男",
      },
      totalElement: 0,
    };
  },
  watch: {
    nowPage(val) {
      this.getData();
      this.setStateData(val);
    },
    limit(val) {
      this.getData();
    },
  },
  methods: {
    setStateData(nowPage) {
      let stateData = {
        nowPage,
      };
      this.$router.replace({
        path: this.$route.path,
        query: {
          stateData: this.$base64.encode(JSON.stringify(stateData)),
        },
      });
    },
    handleSizeChange(val) {
      this.limit = val;
    },
    handleCurrentChange(val) {
      this.nowPage = val;
    },
    modifyEnable(item) {
      ManaApi.global
        .enable({
          targetId: item.id,
          yes: item.enable ? true : false,
        })
        .then((res) => {
          if (res.code == 200) {
            this.$message.success("操作成功");
            this.getData();
          }
        });
    },
    doModify(type, item) {
      if (type == "edit") {
        this.$router.push({
          name: "globalModify",
          query: { type, id: item.id },
        });
      } else {
        this.$confirm("确认此操作?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          ManaApi.global.del({ targetIds: [item.id] }).then((res) => {
            if (res.code == 200) {
              this.$message.success("操作成功");
              this.getData();
            }
          });
        });
      }
    },
    toModify(type) {
      this.$router.push({ name: "globalModify", query: { type } });
    },
    getData() {
      ManaApi.global
        .list({
          limit: this.limit,
          page: this.nowPage - 1,
        })
        .then((res) => {
          this.tableData = res.data;
          this.totalElement = res.total;
          this.showPage = true;
        });
    },
  },
  mounted() {
    if (this.$route.query.stateData) {
      console.log(JSON.parse(this.$base64.decode(this.$route.query.stateData)));
      const stateData = JSON.parse(
        this.$base64.decode(this.$route.query.stateData)
      );
      this.nowPage = stateData.nowPage;
      this.getData();
    } else {
      this.getData();
    }
  },
};
</script>

<style lang="scss" scoped></style>
